<%@ page language="java"%>
<%@ page import="java.io.*" %>
<%@ page import="javax.servlet.ServletRequest"%>
<%@ page import="javax.servlet.http.HttpSession"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%!
	/**
	 * 转换编码，ISO-8859-1 ==> GBK
	 * @param str
	 * @return
	 */
	public static String isoToGBK(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("ISO-8859-1"), "GBK");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("ISO-8859-1 ==> GBK 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， GBK ==> ISO-8859-1 
	 * @param str
	 * @return
	 */
	public static String gbkToISO(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("GBK"), "ISO-8859-1");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println(" GBK ==> ISO-8859-1 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码，ISO-8859-1 ==> UTF-8
	 * @param str
	 * @return
	 */
	public static String isoToUTF(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("ISO-8859-1"), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("ISO-8859-1 ==> UTF-8 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码，系统默认编码 ==> UTF-8
	 * @param str
	 * @return
	 */
	public static String strToUTF(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes(), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("系统默认编码 ==> UTF-8 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码，系统默认编码 ==> GBK
	 * @param str
	 * @return
	 */
	public static String strToGBK(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes(), "GBK");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("系统默认编码 ==> GBK 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， UTF-8 ==> ISO-8859-1
	 * @param str
	 * @return
	 */
	public static String utfToISO(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8 ==> ISO-8859-1 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， UTF-8 ==> gb2312
	 * @param str
	 * @return
	 */
	public static String utfToGB(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("UTF-8"), "gb2312");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8 ==> gb2312 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， UTF-8 ==> GBK
	 * @param str
	 * @return
	 */
	public static String utfToGBK(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("UTF-8"), "GBK");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("UTF-8 ==> GBK 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * 转换编码， GBK ==> UTF-8
	 * @param str
	 * @return
	 */
	public static String gbkToUTF(String str) {
		try {
			if (str != null) {
				str = new String(str.getBytes("GBK"), "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			System.out.println("GBK ==> UTF-8 转换字符编码错误：" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	//获取参数的封装方法
	//获取form提交数据
	private static int getIntParam(ServletRequest request, String sParaName, int iDefaultValue) {
		String sValue = request.getParameter(sParaName);
		try {
			if (sValue==null || sValue.trim().length()<1) {
				return iDefaultValue;				
			} else {
				return Integer.parseInt(sValue.trim());
			}
		} catch(Exception e) {
			e.printStackTrace();
			return iDefaultValue;
		}
	} 		
	private static String getStringParam(ServletRequest request, String sParaName, String sDefaultValue) {
		String sValue = request.getParameter(sParaName);
		if(sValue==null || sValue.trim().length()<1) {
			return sDefaultValue;
		} else {
			return sValue.trim();
		}
	}
	
	//获取session中存储的数据
	private static int getIntParam(HttpSession session, String sParaName, int iDefaultValue) {
		
		try {
			int sValue = (Integer)session.getAttribute(sParaName);
			return sValue;
		} catch(Exception e) {			
			e.printStackTrace();
			return iDefaultValue;
		}
	} 
	
	private static String getStringParam(HttpSession session, String sParaName, String sDefaultValue) {
		String sValue = (String)session.getAttribute(sParaName);
		if(sValue==null || sValue.trim().length()<1)
			return sDefaultValue;
		return sValue.trim();
	}
	
	//URLEncoding
	private static String urlEncode(String str) {
		if (str==null || str.trim().length() < 1) {
			return str;
		}
		try{
	        str = URLEncoder.encode(str, "UTF-8");
	    }catch (Exception e){
	        e.printStackTrace();
	    }
	    return str;
	}

	//URLDecoding
	private static String urlDecode(String str) {
		if (str==null || str.trim().length() < 1) {
			return str;
		}
		try{
	        str = URLDecoder.decode(str, "UTF-8");
	    }catch (Exception e){
	        e.printStackTrace();
	    }
	    return str;
	}
	
	//根据sql预计计算总记录数	
	public static String getPageCountSql(String sql) {
		sql = "SELECT COUNT(*) as count from (" + sql + ") a";
		return sql;
	}
	
	//根据数据库类型，计算sql语句
	public static String getPageBreakSql(String DBType, String sql, int start, int end) {
		String pbSql = sql;		
		if ("MYSQL".equalsIgnoreCase(DBType)) {
			int offset = start - 1;
			if (offset < 0) {
				offset = 0;
			}
			int limit = end - offset;
			pbSql = pbSql + " limit " + offset + ", " + limit;
		} else if ("ORACLE".equalsIgnoreCase(DBType)) {
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT * FROM (");
			sb.append("SELECT A.*, ROWNUM AS RN FROM (");
			sb.append(pbSql);
			sb.append(") A) B ");
			sb.append("WHERE B.RN >= " + start + " AND B.RN <= " + end);
			pbSql = sb.toString();
		}
		return pbSql;
	}
	
	//获取截取固定长度的字串
	public static String getLenStr(String str, int length) {
		if (str != null && str.length() > length) {
			str = str.substring(0, length);
		}
		return str;
	}
 %>
